package org.railage.client.beans;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.railage.common.dto.TripDTO;
import org.railage.common.protocol.Result;
import org.railage.server.services.RouteService;
import org.railage.server.services.TripService;

@ManagedBean(name = "editTrips")
@ViewScoped
public class EditTripsBean {

	private static final Logger log = Logger.getLogger(EditTripsBean.class);

	private TripService tripService = new TripService();
	private RouteService routeService = new RouteService();

	private List<TripDTO> trips = null;
	private String[] routes = null;

	private String selectedRoute;
	private int seatsNumber;
	private Date startDate;
	private Date currentDate = new Date();
	private SimpleDateFormat dateFormatter = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");

	private TripDTO tripToDelete;

	public EditTripsBean() {
		routes = routeService.getRouteNames(null);
		updateTripsArr();
	}

	public void updateTripsArr() {
		trips = Arrays.asList(tripService.getAllTrips(null));
	}

	public void add() {
		Result result = tripService.addTrip(new Object[] { selectedRoute,
				seatsNumber, dateFormatter.format(startDate) });
		updateTripsArr();

		log.debug("Add trip on route: " + selectedRoute + "; Result: "
				+ result.getResult());

		FacesMessage message;
		if (result.isNoError()) {
			message = new FacesMessage(FacesMessage.SEVERITY_INFO,
					result.getResult(), "");
		} else {
			message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					result.getResult(), "");
		}
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void delete() {
		Result result = tripService.deleteTrip(tripToDelete.getId());
		updateTripsArr();

		log.debug("Delete trip: " + tripToDelete.getId() + "; Result: "
				+ result.getResult());

		FacesMessage message;
		if (result.isNoError()) {
			message = new FacesMessage(FacesMessage.SEVERITY_INFO,
					result.getResult(), "");
		} else {
			message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					result.getResult(), "");
		}
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public String getSelectedRoute() {
		return selectedRoute;
	}

	public void setSelectedRoute(String selectedRoute) {
		this.selectedRoute = selectedRoute;
	}

	public int getSeatsNumber() {
		return seatsNumber;
	}

	public void setSeatsNumber(int seatsNumber) {
		this.seatsNumber = seatsNumber;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public TripDTO getTripToDelete() {
		return tripToDelete;
	}

	public void setTripToDelete(TripDTO tripToDelete) {
		this.tripToDelete = tripToDelete;
	}

	public List<TripDTO> getTrips() {
		return trips;
	}

	public String[] getRoutes() {
		return routes;
	}

	public Date getCurrentDate() {
		return currentDate;
	}

}
